﻿@using Jb.Framework.Common.Forms
@model JbForm

@helper WriteSection(JbSection jbSection)
{
    <div class="section-title-bg">
        <h2 class="section-title">@jbSection.Title</h2>
    </div>
    <div class="pull-left section">
        @foreach (var element in jbSection.Elements)
        {
            if (element is JbSection)
            {
            <text>
            @WriteSection(element as JbSection)
            </text>
            }
            else
            {
            @Html.EditorFor(m => element, "JbElement")
            }
        }
    </div>     
}

<div class="jb-form">

    @Html.Hidden("ModelType", Model.GetType())
    @Html.HiddenFor(m => m.Id)
    @Html.HiddenFor(m => m.CurrentMode)
    @Html.HiddenFor(m => m.RefEntityId)
    @Html.HiddenFor(m => m.RefEntityName)

    <div class="section-title-bg">
        <h2 class="section-title">@Model.Title</h2>
    </div>
    <div class="clearfix"></div>
    @if (Model.CurrentMode == AccessMode.ReadOnly)
    {
        @Html.HiddenFor(m => m.JsonElements)
        for (var i = 0; i < Model.Elements.Count(); i++)
        {
            if (Model.Elements[i] is JbSection)
            {
        <text>
        @WriteSection(Model.Elements[i] as JbSection)
        </text>       
            }
            else
            {
        @Html.EditorFor(m => m.Elements[i], "JbElement")  
        <br />
            }
        }
    }
    else
    {
        if (Model.CurrentMode == AccessMode.Design)
        {
        <a  class="formBuilderCaller dropdown-toggle btn btn-info" data-toggle="dropdown" 
                data-JbFormId="@Model.Id"
                data-RefEntityName="@Model.RefEntityName" 
                data-RefEntityId="@Model.RefEntityId"  href="#">Create/Edit Form</a>
        }

        ViewBag.VisibleMode = Model.VisibleMode;

        for (var i = 0; i < Model.Elements.Count(); i++)
        {            
        @Html.EditorFor(m => m.Elements[i])
        }
    }
</div>

<script type="text/javascript">
    //initial popover
    function popoverInit() {
        var items = $("a[rel=\"popover\"]");
        items.popover();
        items.mouseleave(function () {
            items.popover('hide');
        });
    }

    $(".formBuilderCaller").die().live('click', function (e) {
        e.preventDefault();
        var jbFormId = $(this).attr("data-JbFormId");
        var refEntityName = $(this).attr("data-RefEntityName");
        var refEntityId = $(this).attr("data-RefEntityId");
        var url = '@Url.Action("Edit", "FormBuilder")' + '?id=' + jbFormId + '&entityName=' + refEntityName + '&entityId=' + refEntityId;
        CallSecondPopUpData(url, '', 'PopUpJbFormBuilder');
    });

    //$(document).on('submit', function (e) {
    //    //prevent the form from doing a submit
    //    e.preventDefault();
    //    return false;
    //});
    
    //$('form').submit(function (event) {
    //    alert("mn0");
    //    if (!this.checkValidity()) {
    //        event.preventDefault();
    //        alert("mn1");
    //        return false;
    //    }
    //});
</script>
<style>
    .name-info {
        background: url("/images/ico/info.png") no-repeat scroll center bottom rgba(0, 0, 0, 0);
        cursor: pointer;
        display: inline-block;
        padding: 8px;
        vertical-align: top;
    }

    .checkbox .name-info {
        float: left;
    }

    .form-sec .name-info {
        vertical-align: text-top;
    }
</style>
